package com.joseph.test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import javax.tools.FileObject;

/**
 * @description 
 * @author Joseph_Mok
 * @date 2016年3月22日下午2:50:54
 */
public class LogProxy implements InvocationHandler{

	private Object target;
	
	public LogProxy(Object target) {
		this.target = target;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("代理处理前");
		Object result = method.invoke(target, args);  
		System.out.println("代理处理后");
		return result;
	}
	
	public Object getProxy() {
		return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
	}
	
	public static void main(String[] args) throws IOException {
		Movable tank = (Movable) new LogProxy(new Tank()).getProxy();
		FileWriter writer = new FileWriter(new File("C:/proxy.class"));
		tank.run();
	}
}
